package com.usf.jprovider.schema.model.jmodel.item.collection;

import javax.xml.bind.annotation.XmlElement;

import com.usf.jprovider.object.JObjectNode;
import com.usf.jprovider.object.builder.SQLBuilder;
import com.usf.jprovider.object.env.Environment;
import com.usf.jprovider.schema.model.jmodel.item.AbstractItem;
import com.usf.jprovider.schema.model.jmodel.item.field.AbstractField;

public final class Table extends AbstractCollection{

	@XmlElement(name="item") protected AbstractItem[] items;

	public Table() {
		super();
		items = new AbstractItem[]{};
	}
	public AbstractItem[] getItems() {
		return items;
	}
	
	@Override
	protected void _create(Environment env, JObjectNode node) throws Exception {
		for(AbstractItem item : items)
			item.create(env, node);		
	}
	@Override
	protected boolean _check(Environment env, JObjectNode node) throws Exception {
		for(AbstractItem item : items)
			if(!item.check(env, node)) return false;
		return true;
	}
	@Override
	protected void _persist(Environment env, JObjectNode node, SQLBuilder builder) throws Exception {
		builder = new SQLBuilder(name);
		for(AbstractItem item : items){
			if(AbstractField.class.isInstance(item) && item.isPersist())
				item.persist(env, node, builder);
		}
		builder.persist(env.getConnection());
		for(AbstractItem item : items){
			if(AbstractCollection.class.isInstance(item)){
				item.persist(env, node, builder);
			}
		}
	}	
}